package com.example.android.ui.me.user.reserve;

import android.content.res.ColorStateList;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieProperty;
import com.airbnb.lottie.SimpleColorFilter;
import com.airbnb.lottie.model.KeyPath;
import com.airbnb.lottie.value.LottieValueCallback;
import com.example.android.R;
import com.example.android.ui.me.user.merelease.AddReleaseListFragment;
import com.example.android.ui.me.user.merelease.OrderReleaseFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


public class ReserveInfoFragment extends Fragment {
    private TabLayout tab;
    private ViewPager2 page;
    private List<Fragment> fragmentList;

    private View root;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        root = inflater.inflate(R.layout.fragment_reserve_info, container, false);
        //初始化
        init();
        page = root.findViewById(R.id.view_pager);
        //初始化页面加载 1-等待使用2-正在使用 3已经还车  4 -已经取消
        fragmentList = new ArrayList<>();
        fragmentList.add(new ReseverListFragment(0));
        fragmentList.add(new ReseverListFragment(1));
        fragmentList.add(new ReseverListFragment(2));
        fragmentList.add(new ReseverListFragment(3));
        fragmentList.add(new ReseverListFragment(4));
        //设置适配器
        page.setAdapter(new FragmentStateAdapter(requireFragmentManager(), getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getItemCount() {
                return fragmentList.size();
            }
        });
        //设置页面滑动监听
        page.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                tab.getTabAt(position).select();

            }
        });
        return root;
    }

    /**
     * 初始化
     */
    private void init() {
        tab = root.findViewById(R.id.tab_layout8);


        List<String> lottieIcon=new ArrayList<>();
        lottieIcon.add("全部");
        lottieIcon.add("取车中");
        lottieIcon.add("使用中");
        lottieIcon.add("已还车");
        lottieIcon.add("已取消");

        for (String s : lottieIcon) {
            View lottieView = LayoutInflater.from(requireContext()).inflate(R.layout.tab_bar, null);
            LottieAnimationView lottieAnimationImage = lottieView.findViewById(R.id.lav_tab_img);
            lottieAnimationImage.setVisibility(View.GONE);
            TextView text = lottieView.findViewById(R.id.tv_tab_text);
            lottieAnimationImage.setColorFilter(Color.BLUE);
            text.setText(s);
            tab.addTab(tab.newTab().setCustomView(lottieView));
        }
        TabLayout.Tab tabAt = tab.getTabAt(2);
        tabAt.select();
        setSelected(tabAt.getCustomView());
//        tabAt.getCustomView;
        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setSelected(tab.getCustomView());
                page.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                setUnselected(tab.getCustomView());
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    /**
     * 设置点击后的文本颜色
     * @param lottieView
     */
    public void setSelected(View lottieView) {
        TextView textView = lottieView.findViewById(R.id.tv_tab_text);
        int selectColor = ContextCompat.getColor(requireContext(), R.color.colorPrimary);
        textView.setTextColor(selectColor);
        LottieAnimationView iamgeView = lottieView.findViewById(R.id.lav_tab_img);

        if (!iamgeView.isAnimating()) {
            Log.i("TAG", "setSelected: 执行动画");
            iamgeView.playAnimation();
        }
        setLottieColor(iamgeView, true);
    }

    public void setUnselected(View lottieView) {
        TextView textView = lottieView.findViewById(R.id.tv_tab_text);
        int selectColor = ContextCompat.getColor(requireContext(), R.color.black);
        textView.setTextColor(selectColor);
        LottieAnimationView iamgeView = lottieView.findViewById(R.id.lav_tab_img);

        if (iamgeView.isAnimating()) {
            Log.i("TAG", "setUnselected: 取消动画执行");
            iamgeView.cancelAnimation();
            iamgeView.setProgress(0f);//还原状态
        }
        setLottieColor(iamgeView, false);
    }

    private void setLottieColor(LottieAnimationView lottieAnimationView, Boolean isSelected) {
        int color = isSelected ? R.color.colorPrimary : R.color.black;
        ColorStateList csl = AppCompatResources.getColorStateList(requireContext(), color);
        SimpleColorFilter filter = new SimpleColorFilter(csl.getDefaultColor());
        KeyPath keyPath = new KeyPath("**");
        LottieValueCallback<ColorFilter> callback = new LottieValueCallback<>(filter);
        lottieAnimationView.addValueCallback(keyPath, LottieProperty.COLOR_FILTER, callback);

    }
}